package zzs.generalframework.gui.server.core.login;

import static org.hamcrest.CoreMatchers.instanceOf;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.eclipse.wb.swing.FocusTraversalOnArray;

import zzs.generalframework.util.core.string.StringUtils;

import java.awt.Component;
import java.awt.Container;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Window.Type;

/**
 * @title
 * @description
 * @project zzs-gui
 * @package zzs.generalframework.gui.server.core.login
 * @author zzs
 * @date 2018年3月29日
 * @version 1.0
 */
public class LoginFrame extends JFrame
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 310763615785754000L;
	private JPanel loginPanel;
	private JTextField login_text_userName;
	private JPasswordField login_pwd_password;

	/**
	 * 必输项列表
	 */
	private List<Component> mustInputComponetList = new ArrayList<Component>();
	/**
	 * 输入项列表,按焦点顺序
	 */
	private List<Component> inputComponentList = new ArrayList<Component>();

	FocusTraversalPolicy focusTraversalPolicy = null;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{
					LoginFrame frame = new LoginFrame();
					frame.setVisible(true);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame()
	{
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setType(Type.UTILITY);
		setBounds(100, 100, 694, 435);
		loginPanel = new JPanel();

		//loginPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(loginPanel);
		loginPanel.setLayout(null);

		JLabel login_lb_用户名 = new JLabel("用户名:");
		login_lb_用户名.setBounds(159, 100, 54, 24);
		loginPanel.add(login_lb_用户名);

		login_text_userName = new JTextField();
		login_text_userName.setName("用户名");
		login_text_userName.setBounds(257, 102, 238, 21);
		loginPanel.add(login_text_userName);
		login_text_userName.setColumns(10);

		JLabel login_lb_密码 = new JLabel("密码:");
		login_lb_密码.setBounds(159, 158, 54, 15);
		loginPanel.add(login_lb_密码);

		login_pwd_password = new JPasswordField();
		login_pwd_password.setName("密码");
		login_pwd_password.setEchoChar('*');
		login_pwd_password.setBounds(257, 155, 238, 21);
		loginPanel.add(login_pwd_password);

		JButton login_btn_提交 = new JButton("提交");
		login_btn_提交.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				submit();
			}
		});
		login_btn_提交.setBounds(158, 244, 93, 23);
		loginPanel.add(login_btn_提交);

		JButton login_btn_重置 = new JButton("重置");
		login_btn_重置.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				reset();
			}
		});
		login_btn_重置.setBounds(402, 244, 93, 23);
		loginPanel.add(login_btn_重置);
		loginPanel.setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]
		{ login_text_userName, login_pwd_password, login_btn_提交, login_btn_重置 }));
		// 设置必输项
		mustInputComponetList.add(login_text_userName);
		mustInputComponetList.add(login_pwd_password);
		// 设置输入项焦点顺序
		inputComponentList.add(login_text_userName);
		inputComponentList.add(login_pwd_password);
		inputComponentList.add(login_btn_提交);
		inputComponentList.add(login_btn_重置);
		setFocusOrder();
		setMust();
	}

	/**
	 * 设置焦点顺序对象
	 */
	private void setFocusOrder()
	{
		final List<Component> finalComponentList = inputComponentList;
		focusTraversalPolicy = new FocusTraversalPolicy()
		{

			@Override
			public Component getLastComponent(Container aContainer)
			{
				return finalComponentList.get(finalComponentList.size() - 1);
			}

			@Override
			public Component getFirstComponent(Container aContainer)
			{
				return finalComponentList.get(0);
			}

			@Override
			public Component getDefaultComponent(Container aContainer)
			{
				return finalComponentList.get(0);
			}

			@Override
			public Component getComponentBefore(Container rootContainer, Component curComponent)
			{
				int pos = finalComponentList.indexOf(curComponent);
				return finalComponentList.get((pos - 1 + finalComponentList.size()) % finalComponentList.size());
			}

			@Override
			public Component getComponentAfter(Container rootContainer, Component curComponent)
			{
				int pos = finalComponentList.indexOf(curComponent);
				return finalComponentList.get((pos + 1) % finalComponentList.size());
			}
		};

	}

	/**
	 * 设置页面必输项
	 */
	private void setMust()
	{
		if (mustInputComponetList != null && mustInputComponetList.size() > 0)
		{
			for (int i = 0; i < mustInputComponetList.size(); i++)
			{
				Component itemCmp = mustInputComponetList.get(i);
				itemCmp.setBackground(Color.YELLOW);
				FocusAdapter focusAdapter = new FocusAdapter()
				{
					@Override
					public void focusGained(FocusEvent e)
					{
						Component focuceComponet = e.getComponent();
						int curPos = inputComponentList.indexOf(focuceComponet);
						for (int i = 0; i < curPos; i++)
						{
							Component itemComponet = inputComponentList.get(i);
							if (!checkMust(itemComponet))
							{
								break;
							}
						}
					}

					@Override
					public void focusLost(FocusEvent e)
					{
					}
				};
				itemCmp.addFocusListener(focusAdapter);

			}
		}
	}

	/**
	 * 检查页面必输项，如为空，弹框提示,并设置其焦点
	 * 
	 * @param component
	 * @return true:不为空/无需检查,false:为空
	 */
	private boolean checkMust(Component component)
	{
		boolean returnBoolean = true;
		if (mustInputComponetList != null && mustInputComponetList.size() > 0)
		{
			boolean isMustComponent = mustInputComponetList.contains(component);
			if (isMustComponent)
			{
				String inputData = null;
				if (component instanceof JPasswordField)
				{
					inputData = new String(((JPasswordField) component).getPassword());
				}
				else
				{
					inputData = ((JTextField) component).getText();
				}
				if (StringUtils.isNullOrBlank(inputData))
				{
					component.requestFocusInWindow();
					JOptionPane.showMessageDialog(null, component.getName() + "不能为空");
					returnBoolean = false;
				}
			}
		}
		return returnBoolean;
	}

	/**
	 * 检查页面必输项，如为空，弹框提示
	 * 
	 * @return true:不为空/无需检查;false:为空
	 */
	private boolean checkMust()
	{
		boolean returnBoolean = true;
		if (mustInputComponetList != null && mustInputComponetList.size() > 0)
		{
			for (Component itemComponent : inputComponentList)
			{
				if (!checkMust(itemComponent))
				{
					returnBoolean = false;
					break;
				}
			}
		}
		return returnBoolean;
	}

	private void addKeyEvent()
	{
		KeyAdapter keyAdapter = new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				// 回车键
				if (e.getKeyCode() == e.VK_ENTER)
				{

				}
			}
		};
		loginPanel.addKeyListener(keyAdapter);
	}

	/**
	 * 提交操作
	 */
	private void submit()
	{
		boolean checkResult = checkMust();
		if (checkResult)
		{
			String userName = login_text_userName.getText();
			String password = new String(login_pwd_password.getPassword());
			JOptionPane.showMessageDialog(null, "提交用户名:" + userName + ",密码:" + password);
			reset();
		}
	}

	/**
	 * 重置操作
	 */
	private void reset()
	{
		if(inputComponentList != null && inputComponentList.size() > 0)
		{
			for(Component itemComponent : inputComponentList)
			{
				if(itemComponent instanceof JPasswordField)
				{
					((JPasswordField) itemComponent).setText("");
				}
				else
				{
					((JTextField) itemComponent).setText("");
				}
			}
		}
	}
}
